package com.hxzon.aixzsecretary.xml;

import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class AixzMessageProcessor {
	private String filePath = AixzMessageProcessor.class.getResource(
			"/secretary.xml").toString().substring(5);
	private Document document = null;
	private Element root = null;
	private static AixzMessageProcessor instance = null;

	private AixzMessageProcessor() {
	}

	public static AixzMessageProcessor getInstance() {
		if (instance == null)
			return instance = new AixzMessageProcessor();
		return instance;
	}

	public void openXML() {
		try {
			SAXReader reader = new SAXReader();
			this.document = reader.read(this.filePath);
			this.root = document.getRootElement();
		} catch (Exception e) {
			System.out.println("openXML() Exception:" + e.getMessage());
		}
	}// end open

	public void saveXML() {
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");// UTF-8
			XMLWriter output = new XMLWriter(
					new FileOutputStream(this.filePath), format);
			output.write(document);
			output.close();
		} catch (Exception e1) {
			System.out.println("saveXML() Exception:" + e1.getMessage());
		}
	}

	// nodeName cannot be @attribute,cannot be fullPath like childNode/childNode
	public void addElement(String nodeName, String childNodeName,
			String nodeValue) {
		Element node = (Element) document.selectSingleNode("//" + nodeName);
		node.addElement(childNodeName).setText(nodeValue);
	}

	public void addAttribute(String nodeName, String attributeName,
			String attributeValue) {
		Element node = (Element) document.selectSingleNode("//" + nodeName);
		node.addAttribute(attributeName, attributeValue);
	}

	// nodeName can be
	// rootName,nodeName,@attributeName,nodeName/@attributeName,fullPath
	public String getValue(String nodeName) {
		try {
			Node node = document.selectSingleNode("//" + nodeName);
			return node.getText();
		} catch (Exception e1) {
			System.out
					.println("getElementValue() Exception：" + e1.getMessage());
			return null;
		}
	}

	private Element getDateElement(String datetime) {
		datetime = "date[@value='" + datetime + "']";
		try {
			Element element = (Element) document.selectSingleNode("//"
					+ datetime);
			return element;
		} catch (Exception e) {
			System.out.println("getElement() exception:" + e.getMessage());
			return null;
		}
	}

	public Iterator getMessages(String date) {
		Element dateElement = getDateElement(date);
		if (dateElement == null)
			return null;
		try {
			List messages = dateElement.selectNodes("message");
			return messages.iterator();
		} catch (Exception e) {
			System.out.println("getMessages() exception:" + e.getMessage());
			return null;
		}
	}

	// detail can be only
	// title,time,content,@id,@adder,@adddate,@visible,@important
	public String getMessageDetail(Element message, String detail) {
		String title = message.selectSingleNode(detail).getText();
		return title;
	}

	public void addMessage(String date, String id, String adder,
			String addDate, String subject, String content, String time) {
		Element dateElement = getDateElement(date);
		if (dateElement == null) {
			dateElement = root.addElement("date");
			dateElement.addAttribute("value", date);
		}
		Element messageElement = null;
		try {
			messageElement = (Element) dateElement
					.selectSingleNode("message[@id='" + id + "']");
		} catch (Exception e) {
		}
		if (messageElement == null) {// add
			messageElement = dateElement.addElement("message");
			messageElement.addAttribute("id", id);
			messageElement.addAttribute("adder", adder);
			messageElement.addAttribute("addDate", addDate);
			messageElement.addElement("title").addText(subject);
			messageElement.addElement("content").addText(content);
			messageElement.addElement("time").addText(time);
		} else {// modify
			messageElement.selectSingleNode("@id").setText(id);
			messageElement.selectSingleNode("@adder").setText(adder);
			messageElement.selectSingleNode("@addDate").setText(addDate);
			messageElement.selectSingleNode("title").setText(subject);
			messageElement.selectSingleNode("content").setText(content);
			messageElement.selectSingleNode("time").setText(time);
		}
	}

	public static void main(String[] args) {
		AixzMessageProcessor xml = new AixzMessageProcessor();
		xml.openXML();
		// Element date=xml.getDateElement("20080420");//date[@value='20052109']
		Iterator messages = xml.getMessages("20080420");
		while (messages.hasNext()) {
			Element message = (Element) messages.next();
			System.out.println(xml.getMessageDetail(message, "@id"));
		}
		xml.addMessage("20090421", "hxzid", "hxzadd", "addda", "hxzs",
				"hxzcon", "hxztime");
		xml.saveXML();
	}
}
